package com.global.college.utils;


import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;


import com.global.college.constants.Constant;

import java.util.Locale;

/**
 * @author Zhangzs
 * @create 2019/6/10 0010
 * 语言自适应工具
 * 数组资源中设定语言,现为4个类型
 * 1.跟随系统
 * 2.简体中文
 * 3.繁体中文
 * 4.English
 * 然后每次选择的语言都会存入SP中,根据SP中保存的语言类型进行资源语言设置
 * 根据语言的整数值来匹配对应的Locale对象，却省值为简体中文Locale.SIMPLIFIED_CHINESE
 */
public class LangUtils {

    private static Locale getCurrentLang() {
        if (PrefUtils.getString(Constant.Language,"zh").equals("en")) {
            return Locale.ENGLISH;
        } else {
            return Locale.SIMPLIFIED_CHINESE;
        }
    }


    public static Context getAttachBaseContext(Context context) {
//        LogCat.e("pigdreams", "配置语言...默认locale=" + Locale.getDefault() + ";用户设置的为=" + getCurrentLang());
        //Android 7.0之后需要用另一种方式更改res语言,即配置进context中
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context);
        } else {
            //7.0之前的更新语言资源方式
            changeResLanguage(context);
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context) {
        Resources resources = context.getResources();
        Locale locale = getCurrentLang();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    }


    private static void changeResLanguage(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = getCurrentLang();
        configuration.setLocale(locale);
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }
}
